package org.nstack.header.network.l3;

import org.springframework.http.HttpMethod;
import org.nstack.header.identity.Action;
import org.nstack.header.message.APICreateMessage;
import org.nstack.header.message.APIEvent;
import org.nstack.header.message.APIMessage;
import org.nstack.header.message.APIParam;
import org.nstack.header.notification.ApiNotification;
import org.nstack.header.rest.RestRequest;


@Action(category = L3NetworkConstant.ACTION_CATEGORY)
@RestRequest(
        path = "/l3-networks/{l3NetworkUuid}/ip-ranges",
        method = HttpMethod.POST,
        responseClass = APIAddIpRangeEvent.class,
        parameterName = "params"
)
public class APIAddIpRangeMsg extends APICreateMessage implements L3NetworkMessage {

    @APIParam(resourceType = L3NetworkVO.class, checkAccount = true, operationTarget = true)
    private String l3NetworkUuid;

    @APIParam(maxLength = 255)
    private String name;

    @APIParam(required = false, maxLength = 2048)
    private String description;

    @APIParam
    private String startIp;

    @APIParam
    private String endIp;

    @APIParam
    private String netmask;

    @APIParam
    private String gateway;

    @Override
    public String getL3NetworkUuid() {
        return l3NetworkUuid;
    }

    public void setL3NetworkUuid(String l3NetworkUuid) {
        this.l3NetworkUuid = l3NetworkUuid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStartIp() {
        return startIp;
    }

    public void setStartIp(String startIp) {
        this.startIp = startIp;
    }

    public String getEndIp() {
        return endIp;
    }

    public void setEndIp(String endIP) {
        this.endIp = endIP;
    }

    public String getNetmask() {
        return netmask;
    }

    public void setNetmask(String netmask) {
        this.netmask = netmask;
    }

    public String getGateway() {
        return gateway;
    }

    public void setGateway(String gateway) {
        this.gateway = gateway;
    }
 
    public static APIAddIpRangeMsg __example__() {
        APIAddIpRangeMsg msg = new APIAddIpRangeMsg();

        msg.setL3NetworkUuid(uuid());
        msg.setName("Test-IP-Range");
        msg.setStartIp("192.168.100.10");
        msg.setEndIp("192.168.100.250");
        msg.setNetmask("255.255.255.0");
        msg.setGateway("192.168.100.1");

        return msg;
    }

    public ApiNotification __notification__() {
        APIMessage that = this;

        return new ApiNotification() {
            @Override
            public void after(APIEvent evt) {
                ntfy("Added an IP range[(%s~%s)/%s, %s]", startIp, endIp, netmask, gateway)
                        .resource(l3NetworkUuid, L3NetworkVO.class.getSimpleName())
                        .messageAndEvent(that, evt).done();
            }
        };
    }

}
